/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.Drawing;
using TeachMe.Application.Data;
using TeachMe.Application.Drawing;

namespace TeachMe.Application
{
    public class EntryDescriptor
    {
        private readonly Entry _entry;
        private readonly Dictionary _dictionary;
        private readonly RealEntryTextDirection _realDirection;
        private EntryDrawingContext _drawingContext;
        private Point _entryAbsoulteLocation;
        private readonly DateTime _created;
        public DisplayedEntryState DisplayedEntryState { get; set; }
        public int Opacity { get; set; }
        public Point RelativeLocation { get; set; }
        public EntryTextDirection EntryTextDirection { get; set; }

        public EntryDescriptor(NewEntryDescriptorData data)
        {
            _dictionary = data.Dictionary;
            _entry = data.Entry;
            _realDirection = data.RealDirection;
            _created = DateTime.Now;
        }

        public string RealFrom
        {
            get { return Entry.From; }
        }

        public string From
        {
            get { return _realDirection == RealEntryTextDirection.FromTo ? Entry.From : Entry.To; }
        }

        public string To
        {
            get { return _realDirection == RealEntryTextDirection.FromTo ? Entry.To : Entry.From; }
        }

        public string Example
        {
            get { return Entry.Example; }
        }

        public int EntryId
        {
            get { return Entry.Id; }
        }

        public Dictionary Dictionary
        {
            get { return _dictionary; }
        }

        public EntryDrawingContext DrawingContext
        {
            get
            {
                return _drawingContext;
            }
            set
            {
                _drawingContext = value;

                UpdateRectangle();
            }
        }

        public Point AbsoulteLocation
        {
            get { return _entryAbsoulteLocation; }
            set
            {
                _entryAbsoulteLocation = value;
                    
                UpdateRectangle();
            }
        }

        private void UpdateRectangle()
        {
            Rectangle = new Rectangle(_entryAbsoulteLocation, _drawingContext.Size);
        }

        public Rectangle Rectangle { get; private set; }

        public bool IsOnEntry(Point absoluteLocation)
        {
            return Rectangle.Contains(absoluteLocation);
        }

        public bool GetButton(Point absoluteLocation, out EntryButtonType result)
        {
            result = EntryButtonType.Unknown;

            Point relativeLocation = Utilities.SubstractLocations(absoluteLocation, _entryAbsoulteLocation);

            foreach (KeyValuePair<EntryButtonType, ButtonDrawingContext> buttonDrawingContext in _drawingContext.ButtonDrawingContexts)
            {
                if (buttonDrawingContext.Value.Rectangle.Contains(relativeLocation))
                {
                    result = buttonDrawingContext.Key;

                    return true;
                }
            }

            return false;
        }

        public void Draw(Graphics graphics, int progress, bool highlight)
        {
            _drawingContext.Draw(graphics, DisplayedEntryState, progress, highlight);
        }

        public bool Visible { get; set; }
        
        public Entry Entry
        {
            get { return _entry; }
        }

        public DateTime Created
        {
            get { return _created; }
        }

        public void AddButtonState(EntryButtonType buttonType, ButtonState buttonState)
        {
            DisplayedEntryState.ButtonsStates.Add(buttonType, buttonState);
        }

        public void SetButtonState(EntryButtonType buttonType, ButtonState buttonState)
        {
            DisplayedEntryState.ButtonsStates[buttonType] = buttonState;
        }
    }
}
